# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ///////////////////////////////////////////////////////////////
# @File    :   getImageBaseData.py
# @Time    :   2024/08/21 14:39:56
# @Author  :   kai.luo
# @Version :   1.0.0
# @Desc    :   获取医疗图片中头部信息的公共库
# ///////////////////////////////////////////////////////////////

from moudles.Common.common import *
from moudles.MedicalReportForm.commonLibrary.getImageHeaderData import HeaderInfo
import copy


class SichuanHeaderInfo(object):
    """
    姓名,性别,年龄
    病历号,科室,标本情况
    联系电话,采样时间,接收时间
    Args:
        object (_type_): _description_
    """

    def __init__(self, **kwarg) -> None:
        # 初始化对象
        # self.chongqin, self.sichuan = getArea()

        # 初始化参数
        self.commonXList = list()
        self.commonPosValue = list()

        # 初始化公共头部库
        self.comonHeaderInfo = HeaderInfo()

    def getHospitalName(self, data: list):
        hospitalName = self.comonHeaderInfo.getHospitalName(data)

        return hospitalName

    def getHeaderInfo(self, data: list):
        """
        Args:
            data (_type_): _description_
        """
        # print(f'parserHeader---{data}')
        # 先过滤出x轴相同的列数据
        self.getCommonX(data)
        # print(f'self.commonXList----{self.commonXList}')

        # 最后通过获得的列数据,找到与x轴相同y坐标的值
        commonXList = [x for x in self.commonXList if x]
        self.getCommonPosValue(commonXList)
        # print(f'self.commonPosValue----{self.commonPosValue}')

        # 筛选分离出key和value在一起的情况
        header_key_value_list = self.comonHeaderInfo.checkSplitKeyValue(data)
        self.commonPosValue = self.commonPosValue + header_key_value_list

        # 过滤满足显示要求的的键值对
        header_key_value = self.comonHeaderInfo.filterKeyValue(self.commonPosValue)

        return header_key_value

    def getCommonX(self, data: list):
        copy_data = copy.deepcopy(data)
        compare_x = float()
        # compare_y = float()
        temp_data = list()
        for index, item in enumerate(data):
            # text_region = item[1][0]
            x, y = item[0][0]
            if index == 0:
                compare_x = x
                temp_data.append(item)
                copy_data.remove(item)
            else:
                if abs(compare_x-x) <= 10:
                    temp_data.append(item)
                    copy_data.remove(item)

        self.commonXList.append(temp_data)
        if temp_data:
            self.getCommonX(copy_data)

    def getCommonPosValue(self, data: list):
        key_list = data[0]
        copy_data = copy.deepcopy(data[1:])
        if len(data[0]) > 1:  # 默认存在两个元素才是列数据,否则被认为是干扰数据
            for item in key_list:
                key_value_list = list()
                text = item[1][0]
                x, y = item[0][0]

                isBreak = False
                for index, item_vale_list in enumerate(copy_data):
                    if index > 1 or isBreak:
                        break
                    for dict_item in item_vale_list:
                        text_m = dict_item[1][0]
                        x_m, y_m = dict_item[0][0]
                        if abs(y - y_m) <= 6 and abs(x-x_m) <= 110:
                            key_value_list.extend([text, text_m])
                            item_vale_list.remove(dict_item)
                            isBreak = True
                            break
                if key_value_list:  # key值有对应的value
                    # key_value_list.extend([text, ''])
                    self.commonPosValue.append(key_value_list)
                # key和value在一个字符上
                if '：' in text:
                    self.commonPosValue.append(text.split('：'))
        copy_data = [x for x in copy_data if x]
        if len(copy_data) != 0:
            self.getCommonPosValue(copy_data)
